﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SensorNetworkUI;

namespace WebAccess
{
    public partial class Statistics : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Button2.Click += new EventHandler(Button2_Click2);
            
            Button3.Click += new EventHandler(Button3_Click);
            Button4.Click += new EventHandler(Button4_Click);
            //Operation.AutoPostBack = false;
            Operation.SelectedIndexChanged += new EventHandler(Operation_SelectedIndexChanged);

            if (!IsPostBack)
            {
                
                Operation.Items.Clear();
                Operation.Items.Add(new ListItem("Descriptive", "Desc"));
                Operation.Items.Add(new ListItem("Rank Percentile etc", "Rank"));
                Operation.Items.Add(new ListItem("Probability Distributions", "Probability"));
                OpDesc.Items.Add(new ListItem("Mean","Mean"));
                OpDesc.Items.Add(new ListItem("Median", "Median"));
                OpDesc.Items.Add(new ListItem("Mode", "Mode"));
                OpDesc.Items.Add(new ListItem("Standard Deviation", "SD"));
                OpDesc.Items.Add(new ListItem("Min", "Min"));
                OpDesc.Items.Add(new ListItem("Max", "Max"));
                OpDesc.Items.Add(new ListItem("MedianDeviation", "MD"));
                OpDesc.Items.Add(new ListItem("Skewness", "Skewness"));
                ChoiceDesc.Items.Add(new ListItem("Rank", "Rank"));
                ChoiceDesc.Items.Add(new ListItem("Quartile", "Quartile"));
                ChoiceDesc.Items.Add(new ListItem("Percentile", "Percentile"));
                ChoiceDesc.Items.Add(new ListItem("PercentileRank", "PR"));
                PredictDist.Items.Add(new ListItem("Uniform", "Uniform"));
                PredictDist.Items.Add(new ListItem("Normal", "Normal"));
                //PredictDist.Items.Add(new ListItem("Beta", "Beta"));
                //PredictDist.Items.Add(new ListItem("Uniform", "Uniform"));
                //PredictDist.Items.Add(new ListItem("Uniform", "Uniform"));



                Operation.AutoPostBack = true;
            
                UserDataStructure usd = (UserDataStructure)Session["Username"];
                if (usd == null)
                    Response.Redirect("~/login.aspx");
                Sensors.ShowCheckBoxes = TreeNodeTypes.Leaf;
             
                foreach (NetworkDataStructure net in usd.AllowedNetworks)
                {
                    TreeNode CurrentNode = new TreeNode(net.Name, net.Id.ToString());
                    foreach (SensorStructure sens in net.Sensors)
                    {
                        
                        CurrentNode.ChildNodes.Add(new TreeNode(sens.Name, sens.Id.ToString()));

                    }
                    Sensors.Nodes.Add(CurrentNode);



                }
            }
        }

        protected void Sensors_SelectedNodeChanged(object sender, EventArgs e)
        {
            

        }

        protected void Sensors_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
            
            foreach (TreeNode t in e.Node.ChildNodes)
            {
                t.Checked = true;
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            AjaxControlToolkit.AccordionPane pan = new AjaxControlToolkit.AccordionPane();
            pan.HeaderContainer.Controls.Add(new LiteralControl("Chimbs"));
            Panel2.ContentContainer.Visible = true;
            pan.ContentContainer.Controls.Add(Panel2.ContentContainer.Controls[0]);
            
            //pan.HeaderContainer.Controls.Add(panel1.HeaderContainer);
            
            Accordion.Panes.Add(pan);

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            }



        protected HashSet<SensorStructure> CheckedNetworks()
        {
            TreeNodeCollection checkeditems = Sensors.CheckedNodes;
            //List<SensorStructure> selectedsensors = new List<SensorStructure>();
            HashSet<SensorStructure> selectedsensors = new HashSet<SensorStructure>();
            UserDataStructure usd = (UserDataStructure)Session["Username"];

            foreach (NetworkDataStructure net in usd.AllowedNetworks)
            {

                foreach (SensorStructure sens in net.Sensors)
                {

                    TreeNode comp = new TreeNode(sens.Name, sens.Id.ToString());
                    foreach (TreeNode t in checkeditems)
                    {
                        if (comp.Value == t.Value)
                            selectedsensors.Add(sens);

                    }
                }




            }
            return selectedsensors;

        }
        

        protected void Button2_Click1(object sender, EventArgs e)
        {

        }

        protected void Operation_SelectedIndexChanged(object sender, EventArgs e)
        {
           // AjaxControlToolkit.AccordionPane pan = new AjaxControlToolkit.AccordionPane();

            switch (Operation.SelectedValue)
            {
                case "Desc":
                    Descriptive.Visible = true;
                    Rank.Visible = false;
                    Probability.Visible = false;
                    //pan.HeaderContainer.Controls.Add(Descriptive.HeaderContainer.Controls[0]);
                    //pan.ContentContainer.Controls.Add(Descriptive.ContentContainer.Controls[0]);
                    break;
                case "Rank":
                    Rank.Visible = true;
                    Descriptive.Visible = false;
                    Probability.Visible = false;
                    break;
                case "Probability":
                    Probability.Visible = true;
                    Descriptive.Visible = false;
                    Rank.Visible = false;
                    break;

            }
         //   Accordion.Panes.Add(pan);
        }

        protected void OpDesc_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void Operation_SelectedIndexChanged1(object sender, EventArgs e)
        {

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            try
            {
                DescriptiveMethods eng = DescriptiveMethods.GetInstance(OpDesc.SelectedValue);
                eng.Light = RadioButtonList1.SelectedValue.Equals("Light");
                TextBox3.Text = eng.Calculate(CheckedNetworks()).ToString();
            }
            catch (Exception pp)
            {
                TextBox3.Text =pp.Message;
            }
             
        }

        protected void Button2_Click2(object sender, EventArgs e)
        {
            try
            {

                ChoiceStats cho = ChoiceStats.GetInstance(ChoiceDesc.SelectedValue);
                cho.Light = RadioButtonList1.SelectedValue.Equals("Light");
                TextBox2.Text = cho.Calculate(CheckedNetworks(), Double.Parse(TextBox1.Text)).ToString();

            }
            catch
            {


            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            try
            {
                ProbabilityMethods pm = ProbabilityMethods.GetInstance(PredictDist.SelectedValue);
                pm.Light = RadioButtonList1.SelectedValue .Equals( "Light");
                TextBox4.Text = pm.CalculateProbability(CheckedNetworks(), Double.Parse(Rvalue.Text)).ToString() ;
            }
            catch
            {
                TextBox4.Text = "Invalid Operation";
            }
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {

        }

        }
}